package com.jsgt.greekfire.ui.gfc

import android.text.InputFilter
import android.text.TextUtils
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.PriceFormatUtil
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.AssetsBean
import com.jsgt.greekfire.common.util.*
import com.jsgt.greekfire.factory.CoinGFCModelFactory
import com.jsgt.greekfire.viewmodel.CoinCurrencyViewModel
import kotlinx.android.synthetic.main.activity_sale_entrust.*
import org.jetbrains.anko.backgroundColor
import java.math.BigDecimal

class SaleEntrustActivity : BaseActivity(), MyTextWatcher {
    var tvTitle: TextView? = null
    var title_layout: RelativeLayout? = null
    var price: String? = null
    var dealAmount: String? = null

    var assetBalance: String? = null

    override fun getLayoutId(): Int = R.layout.activity_sale_entrust
    private val coinCurrencyViewModel: CoinCurrencyViewModel by lazy {
        ViewModelProvider(
            this,
            CoinGFCModelFactory()
        ).get(CoinCurrencyViewModel::class.java)
    }

    override fun main() {
        NetDataManager.getUserAssets()
        coinCurrencyViewModel.getCoinlistData()
        tvTitle = findViewById(R.id.title_tv)
        etUnitPrice?.requestFocus()
        etUnitPrice?.setFilters(arrayOf<InputFilter>(DecimalDigitsInputFilter(2)))
        title_layout = findViewById(R.id.title_layout)
        tvTitle?.text = resources.getString(R.string.sale_entrust_title)
        title_layout?.backgroundColor = resources.getColor(R.color.pllo_202538)
        coinCurrencyViewModel.coinlistData.observe(this, Observer {
//            tvUnitPrice?.text = it.list!![0].dealAmount!!.toString()
//            tvusdtNun?.text = it.list!![0].dealAmount!!.toString()
//            dealAmount = it.list!![0].dealAmount!!.toString()
            it.result?.forEach { it1 ->
                dealAmount = it1.dealAmount.toString()
                tvusdtNun?.text = dealAmount
                etUnitPrice?.setText(dealAmount)
            }
        })

        etUnitPrice.addTextChangedListener(this)
        etNum.addTextChangedListener(this)
        etPwd.addTextChangedListener(this)
        btnSaleEntrust.setOnClickListener { v ->
            if (!TextUtils.isEmpty(assetBalance)) {
                var assbalance = assetBalance!!.toDouble()
                var price = etUnitPrice.text.toString().trim().toDouble()
                var num = etNum.text.toString().trim().toDouble()
                var balance = UtilsBigDecimal.mul(price, num, BigDecimal.ROUND_DOWN)
                if (balance > assbalance) {
                    ToastUtils.show("可用余额不足")
                    return@setOnClickListener
                }
            }
            coinCurrencyViewModel.getAddAd(
                2, etNum?.text.toString().trim(), etUnitPrice?.text.toString().trim()
                , MD5Util.getMD5(etPwd?.text.toString().trim()), "GFC"
            )
        }
        coinCurrencyViewModel.addAdData.observe(this, Observer {
            finish()
            ToastUtils.show(it.toString())
        })
        coinCurrencyViewModel.errorLiveData.observe(this, Observer {
            ToastUtils.show(it.toString())
        })



        for (bean in NetDataManager.assetsList) {

            if (bean.assetName.equals("USDT")) {
                assetBalance = PriceFormatUtil.format(bean.assetBalance, 4)
                etNum?.setHint(
                    "可用 " + PriceFormatUtil.format(
                        bean.assetBalance,
                        4
                    )
                )
            }
        }

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        price = etUnitPrice.text.toString().trim()
        btnSaleEntrust?.isEnabled = !TextUtils.isEmpty(price) && etNum.text.toString()
            .trim().isNotEmpty() && etPwd.text.toString().trim().isNotEmpty()

    }
}